<?php
include_once(dirname(dirname(__FILE__))."/lib/debug.php");
include_once(dirname(dirname(__FILE__))."/lib/utils.php");
//$arrStr=processSmartyFunction('$keyval|regex_replace:"/(^|[\n ])([\w]+?:\/\/.*?[^ \"\n\r\t<]*)/":"\\1<a href=\"\\2\" target=\"_blank\">\\2</a>"|regex_replace:"/(^|[\n ])((www|ftp)\.[\w\-]+\.[\w\-.\~]+(?:\/[^ \"\t\n\r<]*)?)/":"\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>"|regex_replace:"/(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)/i":"\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"|regex_replace:"/,\"|\.\"|\)\"|\)\.\"|\.\)\"/":"\""|replace:"\n":"<br>&nbsp;"');
//trace($arrStr);
class Smarty2PHP
{
  //private $filePath="";
  protected $tplContent="";
  ///stores the variable defined inside the foreach tag. Useful when converting smarty variable to php variable. while converting, if the foreach variable is defined, the conversion process skipped. If the foreach variable not defined, $this will be added with variable
  private $definedVariable=array();  
  function __construct($tplContent)
  {
    $this->tplContent=$tplContent;        
  }            
  function isLiteralStart($con)
  {
    if(strpos($con, "literal")!==false) return true;
    return false;    
  }
  function isLiteralEnd($con)
  {
    if(strpos($con, "/literal")!==false) return true;
    return false;    
  }
  function isIf($con)
  {
    if(strpos($con, "if")!==false) return true;
    return false;    
  }
  function isIfEnd($con)
  {
    if(strpos($con, "/if")!==false) return true;
    return false;    
  }
  function isSmartyFunction($str)
  {
    $arrVBar=array();
    preg_match_all("/\|.[^ ]/", $str, $arrVBar); 
    if(isset($arrVBar[0][0]))
    {
      return true; 
    }
    return false;
  }
  /*function convertSmartyFunctionToPHP($tag)
  {
    $arrVBar=array(); */                            
    //preg_match_all("/\|.[^ :]*/", $tag, $arrVBar); 
     
    /*if(isset($arrVBar[0][0]))
    {*/    
      //$arrVBar2=preg_split("/\|.[^ :]*/", $tag);
     /* $arrParam=array(); 
      $tmpparam=array_shift($arrVBar2);
      $firstParam=$this->convertSmartyVariableToPHP($tmpparam);
      $function=trim($arrVBar[0][0],"|");  
      $tmpParam=trim($arrVBar2[0]);
      $tmpParam=trim($tmpParam,":");
      $tmpParam=trim($tmpParam);
      $arrTmp=explode(":",$tmpParam);
      //$function=$arrTmp[0];
      if($function=="replace")
        $function="str_replace";
      else if($function=="regex_replace")
        $function="preg_replace";        
      if(count($arrTmp)>0)
      {
        for($vv=0;$vv<count($arrTmp);$vv++)
        {
          if(empty($arrTmp[$vv])) continue;
          $arrParam[]=$arrTmp[$vv];//$this->convertSmartyVariableToPHP($arrTmp[$vv]);
        }
      }  
      if($function=="str_replace" || $function=="preg_replace")
      {
        $arrParam[]=$firstParam;
        $tag=$function.'( '.implode(',',$arrParam).' )';
      }
      else if($function == "cat")
      {
        $tag=implode('.',$arrParam);
      }
      else
      {
        array_unshift($arrParam, $firstParam);
        $tag=$function.'( '.implode(',',$arrParam).' )';
      }
    }
    return $tag;
  }*/
  function convertSmartyFunctionToPHP($tag)
  {      
    $arrTag=splitString("|",$tag);
    $firstParam=array_shift($arrTag);
    $tagcount=count($arrTag);  
    for($i=0;$i<$tagcount;$i++)
    {
      $tg=$arrTag[$i];
      $arrTg=splitString(":",$tg);
      $function=array_shift($arrTg);
      if($function=="replace")
        $function="str_replace";
      else if($function=="regex_replace")
        $function="preg_replace";
      $arrParam=array();
      if(count($arrTg)>0)
      {
        for($vv=0;$vv<count($arrTg);$vv++)
        {
          if(empty($arrTg[$vv])) continue;
          $arrParam[]=$arrTg[$vv];//$this->convertSmartyVariableToPHP($arrTmp[$vv]);
        }
      }  
      
      if($function=="str_replace" || $function=="preg_replace")
      {
        $arrParam[]=$firstParam;
        $firstParam=$function.'( '.implode(',',$arrParam).' )';
      }
      else if($function == "cat")
      {
        $firstParam=implode('.',$arrParam);
      }
      else
      {
        array_unshift($arrParam, $firstParam);
        $firstParam=$function.'( '.implode(',',$arrParam).' )';
      }
    }
    return $firstParam;
  }
  function convertSmartyVariableToPHP($var)
  { 
    $arrVar=array();
    preg_match_all('/\$[a-zA-Z0-9._$]*/', $var, $arrVar);
    $arrSplit=preg_split('/\$[a-zA-Z0-9._$]*/', $var);
      
    $arrVar=$arrVar[0];
    if(empty($arrVar))
    {
      return $var;
    } 
    $finalvar=array_shift($arrSplit);
    foreach($arrVar as $ind=>$variable)
    {      
      if($variable=='$this')
      {
        $phpdata=$variable.array_shift($arrSplit);
      }
      else
      {
        $phpdata="";
        $arrTagDot=explode(".",$variable); 
        if(count($arrTagDot)>1)
        {
          $tmpvar=substr($arrTagDot[0],1);
          if(!in_array($tmpvar, $this->definedVariable))
            $phpdata='$this->'.$tmpvar;
          else
            $phpdata=$arrTagDot[0];
          for($v=1;$v<count($arrTagDot);$v++)
          { 
            if($arrTagDot[$v][0]=='$')
            {
              $arrTagDot[$v]=$this->convertSmartyVariableToPHP($arrTagDot[$v]);
              $phpdata=$phpdata.'['.$arrTagDot[$v].']';
            }
            else
              $phpdata=$phpdata.'["'.$arrTagDot[$v].'"]';          
          }   
        }
        else
        {
          $data=trim($arrTagDot[0]);
          if(!empty($data) && $data[0]=='$')
          {
            $tmpvar=substr($data, 1);
            if(!in_array($tmpvar, $this->definedVariable))
              $phpdata='$this->'.$tmpvar;
            else
              $phpdata=$data;  
          }
          else
            $phpdata=$var;
        }
        $phpdata=$phpdata.array_shift($arrSplit);
        //$finalvar=$finalvar.'$this->'.substr($variable, 1).array_shift($arrSplit);
      }
      $finalvar=$finalvar.$phpdata;    
    }    //if($var=='!empty($CUSTOM_LINKS.DETAILVIEWWIDGET)') trace($finalvar);
    return $finalvar; 
  }
  function splitTag($tag)
  { 
    $arrTag=splitTag($tag); 
    if($arrTag["tag"]=="assign")
    {       
      for($ii=0;$ii<count($arrTag["emptyparam"]);$ii++)
      { 
        $arrTag["param"]["value"]=$arrTag["param"]["value"].$arrTag["emptyparam"][$ii];
      }   
      $arrTag["emptyparam"]=array();     
    }
    else
    {
      $arrTmpEmptyParam=$arrTag["emptyparam"];
      $arrTag["emptyparam"]=array();
      for($ii=0;$ii<count($arrTmpEmptyParam);$ii++)
      {                                 
        $tmp=$arrTmpEmptyParam[$ii];
        $tmp=trim($tmp);
        $prev_index=count($arrTag["emptyparam"])-1;
        ///checking whether smarty function's parameters are separated as token. if so append that with previous   
        $isStoredInPreviousIndex=false;
        if($tmp[0]=="|" && (!isset($tmp[1]) || $tmp[1]!="|"))
        {                
          $arrTag["emptyparam"][$prev_index]=$arrTag["emptyparam"][$prev_index].$arrTmpEmptyParam[$ii]; 
          $isStoredInPreviousIndex=true;
        }          
        $tmplen=strlen($tmp);
        if($tmp[$tmplen-1]==":")
        {
          if($isStoredInPreviousIndex)
          {
            $arrTag["emptyparam"][$prev_index]=$arrTag["emptyparam"][$prev_index].$arrTmpEmptyParam[$ii+1];
          }
          else
          {
            $arrTag["emptyparam"][]=$arrTmpEmptyParam[$ii].$arrTmpEmptyParam[$ii+1];
          }
          $ii=$ii+1;
        }
        else
        {
          $arrTag["emptyparam"][]=$arrTmpEmptyParam[$ii];
        }
      }	
    }
    return $arrTag;
  }
  function convertSmartyToPHP($tag)
  {         
    $tag=trim($tag);
    $tag=trim($tag, '{}');  
    $isIf=false;
    $isFor=false;
    $isForeach=false;
    $isElseif=false;
    $isElse=false;
    
    $isIfEnd=false;
    $isForEnd=false;
    $isForeachEnd=false;  
	
	$arrTag=$this->splitTag($tag);
	$arrTmpEmptyParam=$arrTag["emptyparam"];
	$isEmptyParamModified=false;
	 
	for($iii=0;$iii<count($arrTmpEmptyParam);$iii++)
	{
    if($arrTmpEmptyParam[$iii][0]=='|' && $arrTmpEmptyParam[$iii][1]!='|')
    {
      if(isset($arrTmpEmptyParam[$iii-1]))
      {
        $arrTmpEmptyParam[$iii]=$arrTmpEmptyParam[$iii-1].$arrTmpEmptyParam[$iii];         
        $arrTmpEmptyParam[$iii]=$this->convertSmartyFunctionToPHP($arrTmpEmptyParam[$iii]);
        $arrTmpEmptyParam[$iii]=$this->convertSmartyVariableToPHP($arrTmpEmptyParam[$iii]);
        unset($arrTmpEmptyParam[$iii-1]);  
        $isEmptyParamModified=true;   
      }
      else
      {
        if(isset($arrTag["param"]["value"]))
        {
          $arrTag["param"]["value"]=$arrTag["param"]["value"].$arrTmpEmptyParam[$iii];           
          $arrTag["param"]["value"]=$this->convertSmartyFunctionToPHP($arrTag["param"]["value"]);
          $arrTag["param"]["value"]=$this->convertSmartyVariableToPHP($arrTag["param"]["value"]);
          //trace($arrTag["param"]["value"]);
          unset($arrTmpEmptyParam[$iii]);
          $isEmptyParamModified=true; 
        }
      }
    }
  }
  
  if($isEmptyParamModified)
  {
    $arrTag["emptyparam"]=array();
    foreach($arrTmpEmptyParam as $ind=>$emptyparam)
    {
      $arrTag["emptyparam"][]=$emptyparam;
    }
  } 
   
	$tagname=$arrTag["tag"];
  $type=$arrTag["type"]; 
   
    if($tagname=="if")
    {
      if($type=="open")
      { 
        
        $tagnew=str_replace(" eq ", " == ", $tag);
        $tagnew=str_replace(" eq\r ", " == ", $tagnew);
        $tagnew=str_replace(" eq\n ", " ==\n ", $tagnew);
        $tagnew=str_replace(" neq ", " != ", $tagnew);
        $tagnew=str_replace(" neq\r", " != ", $tagnew);
        $tagnew=str_replace(" neq\n", " !=\n ", $tagnew);
        $tagnew=str_replace(" ne ", " != ", $tagnew);
        $tagnew=str_replace(" ne\r ", " != ", $tagnew);
        $tagnew=str_replace(" ne\n ", " !=\n ", $tagnew);
        
        $tagnew=str_replace(" gt ", " > ", $tagnew);
        $tagnew=str_replace(" gt\r ", " > ", $tagnew);
        $tagnew=str_replace(" gt\n ", " >\n ", $tagnew);
        
        $tagnew=str_replace(" lt ", " < ", $tagnew);
        $tagnew=str_replace(" lt\r ", " < ", $tagnew);
        $tagnew=str_replace(" lt\n ", " <\n ", $tagnew);
        
        $tagnew=str_replace(" mod ", " % ", $tagnew);
        $tagnew=str_replace(" mod\r ", " % ", $tagnew);
        $tagnew=str_replace(" mod\n ", " %\n ", $tagnew); 
        
             
        if(!empty($arrTag["emptyparam"]))
        foreach($arrTag["emptyparam"] as $d)
        {
          if($d=="||") continue;    
          $conv_d=$this->convertSmartyFunctionToPHP($d);  
          $tagnew=str_replace($d, $conv_d, $tagnew);
        }
        $tagnew=$this->convertSmartyVariableToPHP($tagnew);
        $tagnew=str_replace("if ", "if( ", $tagnew);              
        return '<?php '.$tagnew.') { ?'.'>';
      }
      else
      {
        return '<?php } ?'.'>'; 
      }
    }
    else if($tagname=="elseif")
    {
      if($type=="open")
      { 
        $tagnew=str_replace(" eq ", " == ", $tag);
        $tagnew=str_replace(" eq\r ", " == ", $tagnew);
        $tagnew=str_replace(" eq\n ", " ==\n ", $tagnew);
        $tagnew=str_replace(" neq ", " != ", $tagnew);
        $tagnew=str_replace(" neq\r", " != ", $tagnew);
        $tagnew=str_replace(" neq\n", " !=\n ", $tagnew);
        $tagnew=str_replace(" ne ", " != ", $tagnew);
        $tagnew=str_replace(" ne\r ", " != ", $tagnew);
        $tagnew=str_replace(" ne\n ", " !=\n ", $tagnew);        
        
        $tagnew=str_replace(" gt ", " > ", $tagnew);
        $tagnew=str_replace(" gt\r ", " > ", $tagnew);
        $tagnew=str_replace(" gt\n ", " >\n ", $tagnew);
        
        $tagnew=str_replace(" lt ", " < ", $tagnew);
        $tagnew=str_replace(" lt\r ", " < ", $tagnew);
        $tagnew=str_replace(" lt\n ", " <\n ", $tagnew);
        
        $tagnew=str_replace(" mod ", " % ", $tagnew);
        $tagnew=str_replace(" mod\r ", " % ", $tagnew);
        $tagnew=str_replace(" mod\n ", " %\n ", $tagnew);
        if(!empty($arrTag["emptyparam"]))
        foreach($arrTag["emptyparam"] as $d)
        {
          if($d=="||") continue;
          $conv_d=$this->convertSmartyFunctionToPHP($d);  
          $conv_d=$this->convertSmartyVariableToPHP($conv_d); 
          $tagnew=str_replace($d, $conv_d, $tagnew);
        }
        $tagnew=str_replace("elseif ", "else if( ", $tagnew);
                
        return '<?php } '.$tagnew.') { ?'.'>';  
      }
    }
    else if($tagname=="else")
    {
      return '<?php }else{ ?'.'>';
    }
    else if($tagname=="html_options")
    {  //trace($tagname);
      $options=$arrTag["param"]["options"];
      $options=$this->convertSmartyVariableToPHP($options);
      $name=isset($arrTag["param"]["name"])?' name='.$arrTag["param"]["name"]:'';
      $tmp='
      foreach('.$options.' as $__k => $__v)
       {
           echo "<option value=\'{$__v}\'>{$__v}</option>";
       }
       ';
       return '<?php '.$tmp.' ?'.'>';
    }
    else if($tagname=="php")
    {
      if($type=="open")
      {
        return '<?php ';
      }
      else
      {
        return ' ?'.'>';        
      }
    }
    else if($tagname=="php")
    {
      return '';
    }
    else if($tagname=="/section")
    {
      return '<?php } ?'.'>';
    }
    else if($tagname=="foreach")
    {
      if($type=="open")
      {       
        $tmpp=substr($tag, 8);
        $tmpp=trim($tmpp);
        $arrTmpp=explode("=",$tmpp);
        for($ii=0;$ii<count($arrTmpp);$ii++)
        {
          $arrTmpp[$ii]=trim($arrTmpp[$ii]);
        }
        $arrNew=array();
        foreach($arrTmpp as $item)
        {
          $arrItem=explode(" ",$item);
          foreach($arrItem as $it)
          {
            $arrNew[]=$it;
          }
        }
        $arrForeach=array();
        for($mmm=0;$mmm<count($arrNew);$mmm=$mmm+2)
        {
          $arrForeach[$arrNew[$mmm]]=$arrNew[$mmm+1];
        }     
        $arrForeach=$arrTag["param"];
        if(isset($arrTag["param"]["key"]))
        {            
          $tag='';
          if(isset($arrTag["param"]["name"]))
          {
            $arrTag["param"]["name"]=trim($arrTag["param"]["name"],'"');
            $arrTag["param"]["name"]=trim($arrTag["param"]["name"],"'");
            $tag=$tag.'$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]=0;
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]=-1;
            ';    
            //trace($tag);
          }               
          $key=trim($arrTag["param"]["key"]);
          $key=trim($key,'"');
          $key=trim($key,"'");
          
          $item=trim($arrTag["param"]["item"]);
          $item=trim($item,'"');
          $item=trim($item,"'");

          $foreach_var=$this->convertSmartyVariableToPHP($arrForeach["from"]);          
          $tag=$tag.'if(isset('.$foreach_var.') && is_array('.$foreach_var.') && !empty('.$foreach_var.')) 
          {                   
            foreach( '.$foreach_var.' as $'.$key.' => $'.$item.' ) {';
          $this->definedVariable[]=$key;
          $this->definedVariable[]=$item;
          if(isset($arrTag["param"]["name"]))
          {
            $tag=$tag.'
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]+1;
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]+1;
            $this->smarty["foreach"]["foo"]["first"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]===0?true:false;
            $this->smarty["foreach"]["foo"]["last"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]==(count('.$foreach_var.')-1)?true:false;
            ';
          }
        }
        else
        {
          $tag='';
          if(isset($arrTag["param"]["name"]))
          {
            $arrTag["param"]["name"]=trim($arrTag["param"]["name"],'"');
            $arrTag["param"]["name"]=trim($arrTag["param"]["name"],"'");
            $tag=$tag.'$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]=0;
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]=-1;
            ';
          }
          $item=trim($arrTag["param"]["item"]);
          $item=trim($item,'"');
          $item=trim($item,"'");
          $foreach_var=$this->convertSmartyVariableToPHP($arrForeach["from"]);
          $tag=$tag.'if(isset('.$foreach_var.') && is_array('.$foreach_var.') && !empty('.$foreach_var.')) 
          {
            foreach( '.$foreach_var.' as $'.$item.' ) {';
          $this->definedVariable[]=$item;
          if(isset($arrTag["param"]["name"]))
          {
            $tag=$tag.'
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["iteration"]+1;
            $this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]+1;
            $this->smarty["foreach"]["foo"]["first"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]===0?true:false;
            $this->smarty["foreach"]["foo"]["last"]=$this->smarty["foreach"]["'.$arrTag["param"]["name"].'"]["index"]==(count('.$foreach_var.')-1)?true:false;
            ';
          }
        }
        return '<?php '.$tag.' ?'.'>';
        $isForeach=true;
      }
      else
      { 
        return '<?php }
        } ?'.'>'; 
      }
    } 
    else if($tagname=="foreachelse")
    {
      return '<?php 
            }
          }
          else
          {{
          ?'.'>';
    }
    else if($tagname=="section")
    {
      if($type=="open")
      {
        $tmpp=substr($tag, 8);
        $tmpp=trim($tmpp);
        $arrTmpp=explode("=",$tmpp);
        for($ii=0;$ii<count($arrTmpp);$ii++)
        {
          $arrTmpp[$ii]=trim($arrTmpp[$ii]);
        }
        $arrNew=array();
        foreach($arrTmpp as $item)
        {
          $arrItem=explode(" ",$item);
          foreach($arrItem as $it)
          {
            $arrNew[]=$it;
          }
        }
        $arrForeach=array();
        for($mmm=0;$mmm<count($arrNew);$mmm=$mmm+2)
        {
          $arrForeach[$arrNew[$mmm]]=$arrNew[$mmm+1];
        }     
        if(isset($arrForeach["step"]) && isset($arrForeach["start"]))
        {
          $tag='for( $index='.$arrForeach["start"].'; $index<='.$arrForeach["loop"].'; $index=$index+'.$arrForeach["step"].' ) {';
        }
        else if(isset($arrForeach["start"]))
        {
          $tag='for( $index='.$arrForeach["start"].'; $index<='.$arrForeach["loop"].'; $index++ ) {';    
        }    
        else if(isset($arrForeach["step"]))
        {
          $tag='for( $index=0; $index<='.$arrForeach["loop"].'; $index=$index+'.$arrForeach["step"].' ) {';
        }
        else
        {
          $tag='for( $index=0; $index<='.$arrForeach["loop"].'; $index++) {';
        }
        $tag=$tag.'
          $this->smarty["section"]["iter"]["index"]=$index;
          ';
        return '<?php '.$tag.' ?'.'>';   
        $isFor=true;
      }
      else
      {
        return '<?php } ?'.'>'; 
      }
    }
    else if($tagname=="include")
    {   
	  $arrSplit=$this->splitTag($tag);//trace($arrSplit);      
      //$arrTmpp=explode(" file=",$tag);
      return '<?php $this->load('.$arrSplit["param"]["file"].'); ?'.'>';
    }
    else if($tagname=="math")
    {
      if(isset($arrTag["param"]["assign"]))
      {
        $var=trim($arrTag["param"]["assign"]);
        $var=trim($var,'"');
        $var=trim($var,"'");
        $this->definedVariable[]=$var;
        $var='$'.$var.'=';
        unset($arrTag["param"]["assign"]);     
      }
      else
      {
        $var="echo";
      }
      $equation=trim($arrTag["param"]["equation"]);
      $equation=trim($equation,'"');
      $equation=trim($equation,"'");
      unset($arrTag["param"]["equation"]);
      if(!empty($arrTag["param"]))
      {
        foreach($arrTag["param"] as $k=>$v)
        {
          $equation=str_replace($k, $v, $equation);
        }      
      }
      return '<?php '.$var.' '.$equation.'; ?'.'>';
    }
    else if($tagname=="assign")
    { 
             
    	$value=$this->convertSmartyFunctionToPHP($arrTag["param"]["value"]);
    	$value=$this->convertSmartyVariableToPHP($value);
      $var=trim($arrTag["param"]["var"],'"');
      $var=trim($var,"'");
      $tag='$'.$var.' = '.$value.';';
      $this->definedVariable[]=$var;
      return '<?php '.$tag.' ?'.'>';
    }
    else if($tagname=="strip")
    {
      return "";
    }
    else if($tagname=="/if")
    {
      $tag="}";
      $isIfEnd=true;
    }
    else if($tagname=="/for")
    {
      $tag="}";
      $isforEnd=true;
    }
    else if($tagname=="/foreach")
    {
      $tag="}";
      $isForeachEnd=true;
    }
    else if($tagname=="ldelim")
    {
      $tag="{";
      return "{";
    }
    else if($tagname=="rdelim")
    {     
      $tag="}";
      return "}";
    }
    else if($tagname=="/for")
    {
      $tag="}";
      $isforEnd=true;
    }
    else
    {
      //if($tag=='$smarty.request.module|@vtlib_purify')
      //{
        $tag=$this->convertSmartyFunctionToPHP($tag);
        $tag=$this->convertSmartyVariableToPHP($tag);
        //trace($tag);
      //}
      /*$arrVBar=array();
      preg_match_all("/\|.*:/", $tag, $arrVBar);  
      if(isset($arrVBar[0][0]))
      {     
        $arrVBar2=preg_split("/\|.*:/", $tag);
        $param1=array_shift($arrVBar2);
        $param2=array_shift($arrVBar2);
        $function=trim($arrVBar[0][0],"|:");
        $tag=$function.'( '.$param1.' , '.$param2.' )';
      }*/
    }
    
    /*$arrQuoteTag=splitByStringQuotes($tag);
    if(count($arrQuoteTag)>1)
    {
    }
    else
    {
      $arrQuoteTag=splitByStringQuotes($tag,"'");//echo $tag;trace($arrQuoteTag);    
    }  
    $phpdata="";
    for($mm=0;$mm<count($arrQuoteTag);$mm++)
    {
      $arrTmp=explode(" ", $arrQuoteTag[$mm]);
      foreach($arrTmp as $ind=>$tmp)
      {
        if(empty($tmp)) continue;
        if(substr($tmp, 0, 7) != '$this->' && $tmp[0]=='$')
        {
           $arrTmp[$ind]=$this->convertSmartyVariableToPHP($tmp);
        }
      }
      $arrQuoteTag[$mm]=implode(" ",$arrTmp);
    } */
    /*if($isIf || $isFor || $isForeach || $isElseif || $isElse)
    {
      return '<?php '.$tagnew.' ?'.'>';      
    }*/
    if($isIfEnd || $isForEnd || $isForeachEnd)
    {
      return '<?php } ?'.'>';      
    }   
    else
    {
      return '<?php echo '.$tag.'; ?'.'>';
    }
  }
  function render()
  {
    $content=$this->tplContent;//file_get_contents($this->filePath);     
    $arrContent=splitByComment($content,array('{*'));
    $arrF=array();
    $arrF[]='<?php
$this->smarty["request"]=$_REQUEST;
$this->smarty["session"]=$_SESSION;
$this->smarty["server"]=$_SERVER;
?'.'>';
    for($i=0;$i<count($arrContent);$i++)
    {
      if($i%2===1) continue;       
      if(empty($arrContent[$i])) continue;
      $arrSplitContent=splitByTag($arrContent[$i],"literal","{","}");
      for($k=0;$k<count($arrSplitContent);$k++)
      {
        if($k%2===1)
        {
          $arrSplitContent[$k]=str_replace("{literal}", "", $arrSplitContent[$k]);
          $arrSplitContent[$k]=str_replace("{/literal}", "", $arrSplitContent[$k]);
          $arrF[]=$arrSplitContent[$k];
          continue;
        } 
        $arrTmp=array();
        preg_match_all("/{.[^{]*}/", $arrSplitContent[$k], $arrTmp); 
        $arrTmp2=preg_split("/{.[^{]*}/", $arrSplitContent[$k]);
        
        $arrF[]=$arrTmp2[0];
        for($m=0;$m<count($arrTmp[0]);$m++)
        {
          $arrF[]=$this->convertSmartyToPHP($arrTmp[0][$m]);
          $arrF[]=$arrTmp2[$m+1];
        }
      }
    }     
    print_r($this->definedVariable);
    return implode("",$arrF);      
  }      
}
/*function smartytophp($file,$basepath)
{  
  $file=dirname(dirname(__FILE__))."/vtigercrm/Smarty/templates/Login.tpl";
  $arrFilePath=explode($basepath,$file);
  $filepath=$arrFilePath[1];
  $obj=new Smarty2PHP($file);  
  $arrFile=pathinfo($file);           
  $content=$obj->render();
  $finalpath=dirname(dirname(__FILE__))."/AppGen/tpl".$filepath.".php"; // trace($finalpath);
  $finaldir=dirname($finalpath);
  if(!file_exists($finaldir))
  {
    mkdir($finaldir, 0777, true);
  }
  file_put_contents($finalpath, $content);
  exit;
}
get_recursive(dirname(dirname(__FILE__))."/vtigercrm/Smarty/templates","smartytophp");*/
?>